package top.itaso.lib.widget.vo;

import androidx.annotation.IdRes;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 节点实体类
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/12/2
 */
public class Node<T> {
    /**
     * 当前节点id
     */
    private String id;

    /**
     * 父节点id
     */
    private String pid;

    /**
     * 节点名称
     */
    private String title;

    /**
     * 节点图标
     */
    @IdRes
    private int icon = -1;

    /**
     * 当前的级别：
     */
    private int level;

    /**
     * 是否被  checked 选中
     */
    private boolean isChecked;

    /**
     * Item 展开的 指示图标 ，如箭头
     */
    @IdRes
    private int indicativeIconExpand = -1;
    /**
     * Item 非展开的 指示图标 ，如箭头
     */
    @IdRes
    private int indicativeIconUnExpand = -1;

    /**
     * 是否展开
     */
    private boolean isExpand = false;

    /**
     * 节点数据实体类，即 所有数据的 数据源
     */
    private T data;

    /**
     * 父节点
     */
    private Node parent;
    /**
     * 下一级的子 Node
     */
    private List<Node> children = new ArrayList<>();

    public Node() {
    }

    public Node(String id,
                String pid,
                String title,
                @IdRes int icon,
                int level,
                boolean isChecked,
                @IdRes int indicativeIconExpand,
                @IdRes int indicativeIconUnExpand,
                boolean isExpand,
                T data,
                Node parentNode,
                List<Node> children
    ) {
        this.id = id;
        this.pid = pid;
        this.title = title;
        this.icon = icon;
        this.level = level;
        this.isChecked = isChecked;
        this.indicativeIconExpand = indicativeIconExpand;
        this.indicativeIconUnExpand = indicativeIconUnExpand;
        this.isExpand = isExpand;
        this.data = data;
        this.children = children;
        this.parent = parentNode;
    }

    public Node(String id, String pid, String title, List<Node> children) {
        this.id = id;
        this.pid = pid;
        this.title = title;
        this.children = children;
    }

    public Node(String id, String pid, String title, T data) {
        this.id = id;
        this.pid = pid;
        this.title = title;
        this.data = data;
    }

    public Node(String id, String pid, String title) {
        this.id = id;
        this.pid = pid;
        this.title = title;
    }


    /**
     * @return 当前是否为根节点
     */
    public boolean isRoot() {
        return parent == null;
    }

    /**
     * @return 判断父节点是否展开
     */
    public boolean isParentExpand() {
        if (parent == null) {
            return false;
        }
        return parent.isExpand();
    }

    /**
     * @return 当前是否叶子节点，即最后一级
     */
    public boolean isLeaf() {
        return children == null || children.isEmpty();
    }

    /**
     * @return 获取当前节点的级别
     */
    public int getLevel() {
        return parent == null ? 0 : parent.getLevel() + 1;
    }

    /**
     * 设置展开
     *
     * @param isExpand
     */
    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        if (!isExpand) {
            for (Node node : children) {
                node.setExpand(isExpand);
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public @IdRes
    int getIndicativeIconExpand() {
        return indicativeIconExpand;
    }

    public void setIndicativeIconExpand(@IdRes int indicativeIconExpand) {
        this.indicativeIconExpand = indicativeIconExpand;
    }

    public @IdRes
    int getIndicativeIconUnExpand() {
        return indicativeIconUnExpand;
    }

    public void setIndicativeIconUnExpand(@IdRes int indicativeIconUnExpand) {
        this.indicativeIconUnExpand = indicativeIconUnExpand;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public @IdRes
    int getIcon() {
        return icon;
    }

    public void setIcon(@IdRes int icon) {
        this.icon = icon;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    @Override
    public String toString() {
        return "Node{" +
                "id='" + id + '\'' +
                ", pid='" + pid + '\'' +
                ", data=" + data +
                ", children=" + children +
                ", iconExpand=" + indicativeIconExpand +
                ", iconUnExpand=" + indicativeIconUnExpand +
                ", name='" + title + '\'' +
                ", level=" + level +
                ", isExpand=" + isExpand +
                ", icon=" + icon +
                ", parent=" + parent +
                ", isChecked=" + isChecked +
                '}';
    }
}
